package buildinjava.ant;

import buildinjava.Project;
import com.google.common.collect.Maps;
import com.google.inject.Singleton;
import java.util.Map;

/**
 * Global mapping of a project names.
 *
 * @author Gili Tzabari
 */
@Singleton
public final class ProjectProvider
{
	private final Map<String, Project> nameToProject = Maps.newHashMap();

	/**
	 * Returns a project by its name.
	 *
	 * @param name the project name
	 * @return the project, or null if no match is found
	 * @throws IllegalArgumentException if name is null
	 */
	public Project getProject(String name) throws IllegalArgumentException
	{
		if (name == null)
			throw new IllegalArgumentException("name may not be null");
		return nameToProject.get(name);
	}

	/**
	 * Associates a project with a name.
	 *
	 * @param name the project name
	 * @param project the project
	 * @return the previous Project associated with the name, or null if no match is found
	 * @throws IllegalArgumentException if name or project are null
	 */
	public Project putProject(String name, Project project) throws IllegalArgumentException
	{
		if (name == null)
			throw new IllegalArgumentException("name may not be null");
		if (project == null)
			throw new IllegalArgumentException("project may not be null");
		return nameToProject.put(name, project);
	}
}
